<template>
  <div class="list-wrap">
  <G_header title="我的地址" />
    <van-address-list
    v-model="chosenAddressId"
    :list="list"
    @add="onAdd"
    @edit="onEdit"
    @select="changeDefaultAddress"
  />
  </div>
</template>

<script>
import { Toast } from 'vant';
import { AddressList } from 'vant';
export default {
  name: 'List',
  data(){
    return {
      chosenAddressId: null,
      list: []
    }
  },
  created(){
    this.getData()
  },
  methods: {
    getData(){
      this.axios.get('/api/store/user/address/list/?token='+sessionStorage.getItem("token"))
        .then(res=>{
          if(res.data.code ==200){
            let address = res.data.data.data;
            let data = [];
            address.forEach((item,index) => {
              data.push({id:index,name:item.realname,tel:item.mobile,address:`${item.province},${item.city},${item.county},${item.address}`,'R_id':item.id,default:item.default,area:item.area});
            });
            this.list = data;
            this.list.forEach((item,index)=>{
              if(item.default){
                this.chosenAddressId = index
              }
            })
          }
        }).catch(error=>{
          console.log(error)
        })
    },
    onAdd() {
      this.$router.push({name: 'addressAddressList',path:'../addressList',query:{type:'add'}});
    },
    onEdit(item, index){
      this.global.state.editAddressData = item;
      this.$router.push({name: 'addressAddressList',path:'../addressList',query:{type:'edit'}});
    },
    async changeDefaultAddress(val){
      return
      Toast.loading({
        message: '加载中...',
        forbidClick: true,
        loadingType: 'spinner',
        duration: 0, // 持续展示 toast
      });
      let { R_id } = val;
      const res = await this.axios.post(`/api/store/user/address/edit/${R_id}/?token=${sessionStorage.getItem("token")}`,{
        default: 1
      });
      console.log(res);
      
      const defaultAddress = await this.axios.get('/api/store/user/address/list/?token=' + sessionStorage.getItem("token"));
            defaultAddress.then(res => {
                res.data.data.data.forEach(item => {
                    if (item.default) {
                        this.global.state.defaultAddress = item
                    }
                })
            })
            Toast('切换默认地址成功');
      return
      this.axios.post('/api/store/user/address/edit/' + this.address_id + '?token=' + sessionStorage.getItem("token"), )
              .then(res => {
                  if (res.data.code == 200) {
                      Notify({
                          type: 'success',
                          background: '#07c160',
                          message: res.data.message,
                          onOpened: () => {
                              this.$router.go(-1)
                          }
                      });
                  }
              }).catch(error => {
                  console.log(error)
              })
      
    },
  }
}
</script>
<style  scoped></style>
